jsx3.require("tibco.admin.enterpriseAssets.EnterpriseAssetsTabInterface");

/**
 * Def Connectors
 * author:Felix Woo
 * Date: 5/8/2007
 */
jsx3.lang.Package.definePackage("tibco.admin.environments.defConn", function (defConn) {
});

/**
 * Def Connectors
 * author:Felix Woo
 * Date: 5/8/2007
 */
jsx3.lang.Class.defineClass("tibco.admin.environments.defConn.DefConn",
        jsx3.lang.Object,
        [tibco.admin.environments.EnvironmentsInterface],
        function (plugin) {

            plugin.instance = null

            plugin.prototype.init = function() {
                this.jsxsuper();
                tibco.admin.Main.loadResource("tibco/admin/environments/defConn/jss/jss.xml", "tibco.admin", "jss32", "ljss", false);
            }

            // interface method
            plugin.prototype.getNamespace = function() {
                return "tibco.admin";
            }

            //interface method
            plugin.prototype.destroyPlugin = function() {
            }

            //interface method, return either static instance or new instance is upto
            // implementation requirement
            plugin.getInstance = function() {
                if(!this.instance) {
                    this.instance = new tibco.admin.environments.defConn.DefConn();
                }
                return this.instance;
            }

            // this method need to satisfy the tibco.admin.plugin.PluginInterface
            // Using this optional.
            plugin.prototype.loadComponent = function(parent) {
                if(!this.loading) {
                    this.loading = true;
                    var me = this;
                    tibco.admin.Main.loadUIResourceAsync(parent, this.getNamespace(), "tibco/admin/environments/defConn/ui/defConn.xml", me, me.componentLoaded);
                }
            }

            plugin.prototype.onAfterComponentUnloaded = function() {
                if(this.isInEditMode()) {
                    this.endEditMode();
                }
                this.envId = null;
                this.connectorId = null;
                this.connectorName = null;
                this.nodeId = null;
                this.block = null;
                if(this.loading) {
                    delete this.loading;
                }
            }

            plugin.prototype.componentLoaded = function(objEvent) {
                delete this.loading;
                if(objEvent.status == "SUCCESS") {
                    this.block = objEvent.obj;
                    this.block.getDescendantOfName("btnEdit_32").setEnabled((this.nodeState == "Starting") ? jsx3.gui.Form.STATEDISABLED : jsx3.gui.Form.STATEENABLED, true);
                    var me = this;
                    this.block.handler = me;
                    this.loadDetails();
                }
                else {
                    if(objEvent.exception)
                    {
                        tibco.admin.Main.logException(objEvent.exception, objEvent.message);
                    }
                    else {
                        jsx3.log(objEvent.message);
                    }
                }
            }

            plugin.prototype.getButtonText = function(parent) {
                return tibco.admin.Main.getServer().getDynamicProperty("@@lblSectionTitle_32");
            }

            plugin.prototype.setEnvId = function(id) {
                this.envId = id;
            }

            plugin.prototype.setNodeState = function(nodeState) {
                this.nodeState = nodeState;
                if(this.block) {
                    this.block.getDescendantOfName("btnEdit_32").setEnabled((this.nodeState == "Starting") ? jsx3.gui.Form.STATEDISABLED : jsx3.gui.Form.STATEENABLED, true);
                }
            }

            plugin.prototype.setNodeIds = function(arrIds) {
                if(this.isInEditMode()) {
                    if((arrIds && (arrIds.length == 1) && (arrIds[0] != this.nodeId)) || (this.nodeId && (!arrIds || (arrIds.length != 1)))) {
                        tibco.admin.Main.alert(tibco.admin.Main.getServer().getDynamicProperty("@@msgCannotUpdateNodeIdTitle_32"), tibco.admin.Main.getServer().getDynamicProperty("@@msgCannotUpdateNodeIdMessage_32"));
                    }
                    return;
                }
                if(arrIds && (arrIds.length == 1)) {
                    this.nodeId = arrIds[0];
                }
                else {
                    this.nodeId = null;
                }
                if(this.nodeId && this.block) {
                    this.loadDetails();
                }
            }

            plugin.prototype.loadDetails = function() {
                try {
                    var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/environments/defConn/rules/getNodeDetails.xml", "getNodeDetails");
                    if(tibco.admin.Main.getServer().getEnv("MODE")) {
                        objService.setEndpointURL(tibco.admin.util.ServiceHelper.getNodeEndpointURL());
                    }

                    //subscribe and call
                    var me = this;
                    objService.nodeId = this.nodeId;
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, me, me.onloadDetailsSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, me, me.onloadDetailsError);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, me, me.onloadDetailsInvalid);
                    objService.doCall();
                }
                catch(ex) {
                    tibco.admin.Main.logException(ex, "Error in tibco.admin.environments.defConn.DefConn.loadDetails");
                }
            }

            plugin.prototype.onloadDetailsSuccess = function(objEvent) {
                var doc = tibco.admin.Main.getServer().getCache().getDocument("getNodeDetails_32");
                if(doc) {
                    this.connectorId = doc.getAttribute("EntityID");
                    this.connectorName = doc.getAttribute("name");
                    this.connectorStatus = doc.getAttribute("platformResourceState");
                    if(this.block) {
						if(tibco.admin.Main.getServer().getDynamicProperty("@addin@State " + this.connectorStatus) != "" && tibco.admin.Main.getServer().getDynamicProperty("@addin@State " + this.connectorStatus) != null && tibco.admin.Main.getServer().getDynamicProperty("@addin@State " + this.connectorStatus) != undefined){
							this.block.getDescendantOfName("lblStatusValue_32").setText(tibco.admin.Main.getServer().getDynamicProperty("@addin@State " + this.connectorStatus), true);
						}
                        this.callgetHttpResourceDetails();
                    }
                }
            }

            plugin.prototype.onloadDetailsError = function(objEvent) {
            }

            plugin.prototype.onloadDetailsInvalid = function(objEvent) {
            }

            /**
             * Event handler for Edit button
             *
             */
            plugin.btnEdit_onclick = function() {
                plugin.getInstance().beginEditMode();
                // hide labels, show textBoxs;
                plugin.visitTextbox(
                        function(textBox, label) {
                            if(textBox && label) {
                                textBox.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                                label.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                            }
                        }
                        )

                var server = tibco.admin.Main.getServer();
                server.getJSXByName("btnSave_32").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                server.getJSXByName("btnCancel_32").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                server.getJSXByName("btnEdit_32").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                plugin.getInstance().loadDetails();
            }
            /**
             * Event handler for Cancel button
             *
             */
            plugin.btnCancel_onclick = function() {
                /*
            tibco.admin.Main.confirm(tibco.admin.Main.getServer().getDynamicProperty("@@ConfirmTitle_32"), tibco.admin.Main.getServer().getDynamicProperty("@@ConfirmMessage_32"), function(confirmationDialog) {
                plugin.visitTextbox(
                function(textBox, label){
                    if(textBox && label){
                        textBox.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                        label.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                    }
                }
                );
                var server = tibco.admin.Main.getServer();

                server.getJSXByName("btnSave_32").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                server.getJSXByName("btnCancel_32").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                server.getJSXByName("btnEdit_32").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                plugin.getInstance().endEditMode();
                plugin.getInstance().loadDetails();
                confirmationDialog.doClose();
                }, function(confirmationDialog){
                confirmationDialog.doClose();
                },
                tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn Yes"), tibco.admin.Main.getServer().getDynamicProperty("@addin@Btn No"), 2, null, null, "");
                */
                plugin.getInstance().doCancelEdits();
            }

            plugin.prototype.doCancelEdits = function() {
                plugin.visitTextbox(
                        function(textBox, label) {
                            if(textBox && label) {
                                textBox.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                                label.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                            }
                        }
                        );
                var server = tibco.admin.Main.getServer();

                server.getJSXByName("btnSave_32").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                server.getJSXByName("btnCancel_32").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                server.getJSXByName("btnEdit_32").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                this.endEditMode();
                this.loadDetails();

            }

            plugin.prototype.getConfirmText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@@Confirm Discard Text_32");
            }

            /**
             * Event handler for Save button
             *
             */
            plugin.btnSave_onclick = function() {
                plugin.getInstance().calleditHttpResource();
            }

            /**
             * Visit all the textbox and its label buddle. Apply function on them.
             * fctn(textBox, label) specified the operation on textBox and label
             */
            plugin.visitTextbox = function(fctn) {
                var root = tibco.admin.Main.getServer().getRootBlock();
                var arr = root.findDescendants(function(o) {
                    return o.getName().substr(0, 3) == 'txt'
                }, true, true)
                for(var i = 0; i < arr.length; i++) {

                    var name = arr[i].getName();
                    var label = root.getDescendantOfName("lbl" + name.substr(3, (name.indexOf('_') - 3)) + "Value_" + name.substr(name.indexOf('_') + 1));
                    fctn(arr[i], label);
                }
            }


            //call this method to begin the service call (eg.service.callgetHttpResourceDetails();)
            plugin.prototype.callgetHttpResourceDetails = function() {
                var objService = tibco.admin.Main.loadService(this.getNamespace(), "tibco/admin/environments/defConn/rules/getHttpResourceDetails.xml", "getHttpResourceDetails", "getHttpResourceDetails");
                if(tibco.admin.Main.getServer().getEnv("MODE")) {
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSharedResourcesEndpointURL());
                }
                //subscribe and call
                objService.connectorName = this.connectorName;
                objService.connectorId = this.connectorId;
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, plugin.ongetHttpResourceDetailsSuccess);
                objService.subscribe(jsx3.net.Service.ON_ERROR, plugin.ongetHttpResourceDetailsError);
                objService.subscribe(jsx3.net.Service.ON_INVALID, plugin.ongetHttpResourceDetailsInvalid);
                objService.setInboundURL("tibco/admin/environments/defConn/xml/getHttpResourceDetails.xml");
                objService.doCall();
            };

            plugin.ongetHttpResourceDetailsSuccess = function(objEvent) {
                var me = plugin.getInstance();
                var doc = tibco.admin.Main.getServer().getCache().getDocument("getHttpResourceDetails_32");
                if(doc) {
                    var host = doc.getAttribute("host");
                    var port = doc.getAttribute("port");
                    var enableDNSLookup = doc.getAttribute("enableDNSLookup");
                    var maxPostSize = doc.getAttribute("maxPostSize");
                    var redirectPort = doc.getAttribute("redirectPort");
                    var connectionTimeout = doc.getAttribute("connectionTimeout");
                    var minThreads = doc.getAttribute("minThreads");
                    var maxThreads = doc.getAttribute("maxThreads");
                    var disableUploadTime = doc.getAttribute("disableUploadTime");
                    if(me.isInEditMode()) {
                        me.block.getDescendantOfName("txtHost_32").setValue(host, true);
                        me.block.getDescendantOfName("txtPort_32").setValue(port, true);
                        var rad1 = (enableDNSLookup && (enableDNSLookup == "true")) ? me.block.getDescendantOfName("radEDLYes_32") : me.block.getDescendantOfName("radEDLNo_32");
                        rad1.setSelected(jsx3.gui.RadioButton.SELECTED);
                        me.block.getDescendantOfName("txtMPS_32").setValue(maxPostSize ? maxPostSize : "", true);
                        me.block.getDescendantOfName("txtRePort_32").setValue(redirectPort ? redirectPort : "", true);
                        me.block.getDescendantOfName("txtConnTO_32").setValue(connectionTimeout ? connectionTimeout : "", true);
                        me.block.getDescendantOfName("txtMinTh_32").setValue(minThreads ? minThreads : "", true);
                        me.block.getDescendantOfName("txtMaxTh_32").setValue(maxThreads ? maxThreads : "", true);
                        var rad2 = (disableUploadTime && (disableUploadTime == "true")) ? me.block.getDescendantOfName("radDUTYes_32") : me.block.getDescendantOfName("radDUTNo_32");
                        rad2.setSelected(jsx3.gui.RadioButton.SELECTED);
                    }
                    else {
                        me.block.getDescendantOfName("lblHostValue_32").setText(host, true);
                        me.block.getDescendantOfName("lblPortValue_32").setText(port, true);
                        me.block.getDescendantOfName("lblEDLValue_32").setText((enableDNSLookup && (enableDNSLookup == "true")) ? tibco.admin.Main.getServer().getDynamicProperty("@addin@radBtn Yes") : tibco.admin.Main.getServer().getDynamicProperty("@addin@radBtn No"), true);
                        me.block.getDescendantOfName("lblMPSValue_32").setText(maxPostSize ? maxPostSize : "", true);
                        me.block.getDescendantOfName("lblRePortValue_32").setText(redirectPort ? redirectPort : "", true);
                        me.block.getDescendantOfName("lblConnTOValue_32").setText(connectionTimeout ? connectionTimeout : "", true);
                        me.block.getDescendantOfName("lblMinThValue_32").setText(minThreads ? minThreads : "", true);
                        me.block.getDescendantOfName("lblMaxThValue_32").setText(maxThreads ? maxThreads : "", true);
                        me.block.getDescendantOfName("lblDUTValue_32").setText((disableUploadTime && (disableUploadTime == "true")) ? tibco.admin.Main.getServer().getDynamicProperty("@addin@radBtn Yes") : tibco.admin.Main.getServer().getDynamicProperty("@addin@radBtn No"), true);
                    }
                }
            };

            plugin.ongetHttpResourceDetailsError = function(objEvent) {
            };

            plugin.ongetHttpResourceDetailsInvalid = function(objEvent) {
            };

            //call this method to begin the service call (eg.service.calleditHttpResource();)
            plugin.prototype.calleditHttpResource = function() {
                var objService = tibco.admin.Main.loadService(this.getNamespace(), "tibco/admin/environments/defConn/rules/editHttpResource.xml", "editHttpResource", "editHttpResource");
                if(tibco.admin.Main.getServer().getEnv("MODE")) {
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSharedResourcesEndpointURL());
                }

                objService.connectorName = this.connectorName;
                objService.connectorId = this.connectorId;
                objService.enableDNSLookup = this.block.getDescendantOfName("radEDLYes_32").getSelected() == jsx3.gui.RadioButton.SELECTED;
                objService.disableUploadTime = this.block.getDescendantOfName("radDUTYes_32").getSelected() == jsx3.gui.RadioButton.SELECTED;

                //subscribe and call
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, plugin.oneditHttpResourceSuccess);
                objService.subscribe(jsx3.net.Service.ON_ERROR, plugin.oneditHttpResourceError);
                objService.subscribe(jsx3.net.Service.ON_INVALID, plugin.oneditHttpResourceInvalid);
                objService.setInboundURL("tibco/admin/environments/defConn/xml/editHttpResource.xml");
                objService.doCall();
            };

            plugin.oneditHttpResourceSuccess = function(objEvent) {
                var server = tibco.admin.Main.getServer();
                server.getJSXByName("btnSave_32").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                server.getJSXByName("btnCancel_32").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                server.getJSXByName("btnEdit_32").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                plugin.visitTextbox(
                        function(textBox, label) {
                            if(textBox && label) {
                                textBox.setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                                label.setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                            }
                        }
                        )
                plugin.getInstance().endEditMode();
                plugin.getInstance().loadDetails();
            };

            plugin.oneditHttpResourceError = function(objEvent) {
            };

            plugin.oneditHttpResourceInvalid = function(objEvent) {
            };

            plugin.prototype.getComponentPath = function() {
                return "tibco/admin/environments/defConn/component.xml"
            }

            plugin.prototype.buttonSelected = function() {
                if(!this.isInEditMode()) {
                    this.loadDetails();
                }
            }
        }
        );